package simpleflink;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

public class TransformTest01 {

    public static void main(String[] args) throws Exception {
        //创建执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //从文件中读取
        DataStream<String> dataStream = env.readTextFile("data/user.txt");

        DataStream<String> mapStream = dataStream.map(new MapFunction<String, String>() {
            public String map(String s) throws Exception {
                return "love_"+s;
            }
        });

        mapStream.print("map");

        DataStream<String> flatMapStream = mapStream.flatMap(new FlatMapFunction<String, String>() {
            public void flatMap(String s, Collector<String> collector) throws Exception {
                String[] words = s.split(" ");
                for(String word : words){
                    collector.collect(word);
                }
            }
        });

        flatMapStream.print("flatMap");

        DataStream<String> filterStream = flatMapStream.filter(new FilterFunction<String>() {
            public boolean filter(String s) throws Exception {
                return s.length() > 20;
            }
        });

        filterStream.print("filter");

        env.execute();
    }
}
